package com.example.administrator.myapplication.Activite;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.TextView;

import com.example.administrator.myapplication.R;

public class MainActivity extends Activity implements OnClickListener {
    private WindowManager mWindowManager;
    private View myView;
    private TextView btn_dayAndnight;
    private Button chaxun;
    private SharedPreferences skinSp;
    private final static String DAY = "day";
    private final static String NIGHT = "night";
    private int flage = 0;
    private WebView webview;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mWindowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
        setContentView(R.layout.activity_main);
        chaxun = (Button) findViewById(R.id.chaxun);
        chaxun.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                progress();


            }
        });

        init();
wangluo();
    }
public void wangluo(){
    //实例化WebView对象
     webview = (WebView) findViewById(R.id.webview);
    //设置WebView属性，能够执行Javascript脚本
      webview.getSettings().setJavaScriptEnabled(true);
    //加载需要显示的网页
      webview.loadUrl("http://www.baidu.com/");
    //设置Web视图
    setContentView(webview);
}
    @Override
    //设置回退
    //覆盖Activity类的onKeyDown(int keyCoder,KeyEvent event)方法
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) {
            webview.goBack(); //goBack()表示返回WebView的上一页面
            return true;
        }
        return false;
    }


    //模式切换
    private void init() {
        skinSp = this.getSharedPreferences("skinchange", Context.MODE_PRIVATE);
        btn_dayAndnight = (TextView) findViewById(R.id.btn_dayAndnight);
        btn_dayAndnight.setOnClickListener(this);

        String mode = skinSp.getString("skin", "");
        if(mode!=null||!mode.equals("")){
            if(mode.equals(NIGHT)){
                night();
            }else{
                day();
            }
        }

    }
    @Override
    public void onClick(View v) {
        if(flage%2==0){
            night();
            btn_dayAndnight.setText("白天模式");
            btn_dayAndnight.setTextColor(Color.WHITE);
            flage++;
        }else{
            day();
            btn_dayAndnight.setText("夜间模式");
            btn_dayAndnight.setTextColor(Color.BLACK);
            flage++;
        }
    }
    public void night() {
        WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT,
                LayoutParams.TYPE_APPLICATION,
                WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
                        | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSLUCENT);
        params.gravity=Gravity.BOTTOM;
        params.y=10;
        if(myView==null){
            myView=new TextView(this);
            myView.setBackgroundColor(0x80000000);
        }
        mWindowManager.addView(myView, params);
        Editor edit = skinSp.edit();
        edit.putString("skin", NIGHT);
        edit.commit();
    }

    public void day(){
        if(myView!=null){
            mWindowManager.removeView(myView);
            Editor edit = skinSp.edit();
            edit.putString("skin", DAY);
            edit.commit();
        }
    }


    public void removeSkin(){
        if(myView!=null){
            mWindowManager.removeView(myView);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        String mode = skinSp.getString("skin", "");
        if(mode.equals(NIGHT)){
            removeSkin();
        }
    }
    /**
     * 模式切换
     */


    public void progress(){

/*注意这里声明对话框的方式与其他的对话框的区别*/
        ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setTitle("稍等.....");
//        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        /*这里表示添加最大进度，后面可以通过setprogress方法修改进度*/
        progressDialog.setMax(100);
        /*这里的button可以随便set，不限制个数*/
        progressDialog.setButton("取消", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface Dialog, int which) {
                //这里添加点击这个按钮后的事件
            }
        });
//        progressDialog.setButton2("Cancel",
//                new DialogInterface.OnClickListener() {
//
//                    @Override
//                    public void onClick(DialogInterface arg0, int arg1) {
//                        //这里添加点击这个按钮后的事件
//                    }
//                });

        progressDialog.show();


    }

}